package biolabs.space;

class BooleanViewGrid<T, P extends DiscretePosition<P,C>, C extends ContinuousPosition<C,P>>
		extends Grid<Boolean, P, C> {

	public interface Predicate<T> {
		boolean asBoolean(T obj);
	}

	private final Grid<T, P, C> targetGrid;
	private final Predicate<? super T> predicate;

	public BooleanViewGrid(Grid<T, P, C> targetGrid, Predicate<? super T> tr){
		this.targetGrid = targetGrid;
		this.predicate = tr;
	}

	@Override
	public Boolean at(P pos) {
		return predicate.asBoolean(targetGrid.at(pos));
	}

	@Override
	public P getSize() {
		return targetGrid.getSize();
	}

	@Override
	public boolean inRange(P pos) {
		return targetGrid.inRange(pos);
	}

	/**
	 * Modification is not supported by this class.
	 * @throws UnsupportedOperationException always.
	 */
	@Override
	protected Boolean _setField(P pos, Boolean value) {
		throw new UnsupportedOperationException("The grid cannot be modified through its boolean view");
	}

	@Override
	public Grid<Boolean, P, C> copyOf(P origin, P size) {
		return new GenericGrid<Boolean, P, C>(this, origin, size);
	}

	/**
	 * Modification is not supported by this class.
	 * @throws UnsupportedOperationException always.
	 */
	@Override
	public void fill(Boolean value) {
		throw new UnsupportedOperationException("The grid cannot be modified through its boolean view");
	}
}
